package com.scheduling.machine.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scheduling.common.result.Result;
import com.scheduling.machine.dto.MachineTypeDTO;
import com.scheduling.machine.dto.MachineTypeQueryDTO;
import com.scheduling.machine.entity.MachineType;
import com.scheduling.machine.service.MachineTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 机器类型管理控制器
 *
 * @author 开发团队
 * @since 2025-05-29
 */
@Slf4j
@RestController
@RequestMapping("/api/machine-type")
@Tag(name = "机器类型管理", description = "机器类型信息的增删改查操作")
public class MachineTypeController {

    @Autowired
    private MachineTypeService machineTypeService;

    @Operation(summary = "分页查询机器类型列表", description = "根据条件分页查询机器类型信息")
    @PostMapping("/page")
    public Result<IPage<MachineType>> getMachineTypePage(@RequestBody MachineTypeQueryDTO queryDTO) {
        try {
            IPage<MachineType> page = machineTypeService.getMachineTypePage(queryDTO);
            return Result.success(page);
        } catch (Exception e) {
            log.error("分页查询机器类型列表失败：{}", e.getMessage(), e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @Operation(summary = "根据ID获取机器类型详情", description = "根据机器类型ID获取详细信息")
    @GetMapping("/{machineTypeId}")
    public Result<MachineType> getMachineTypeById(
            @Parameter(description = "机器类型ID", required = true) @PathVariable String machineTypeId) {
        try {
            MachineType machineType = machineTypeService.getMachineTypeById(machineTypeId);
            if (machineType == null) {
                return Result.error("机器类型不存在");
            }
            return Result.success(machineType);
        } catch (Exception e) {
            log.error("获取机器类型详情失败：{}", e.getMessage(), e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    @Operation(summary = "添加机器类型", description = "添加新的机器类型信息")
    @PostMapping
    public Result<String> addMachineType(@Validated @RequestBody MachineTypeDTO machineTypeDTO) {
        try {
            boolean success = machineTypeService.addMachineType(machineTypeDTO);
            if (success) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        } catch (Exception e) {
            log.error("添加机器类型失败：{}", e.getMessage(), e);
            return Result.error("添加失败：" + e.getMessage());
        }
    }

    @Operation(summary = "更新机器类型", description = "更新机器类型信息")
    @PutMapping
    public Result<String> updateMachineType(@Validated @RequestBody MachineTypeDTO machineTypeDTO) {
        try {
            boolean success = machineTypeService.updateMachineType(machineTypeDTO);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            log.error("更新机器类型失败：{}", e.getMessage(), e);
            return Result.error("更新失败：" + e.getMessage());
        }
    }

    @Operation(summary = "删除机器类型", description = "根据ID删除机器类型")
    @DeleteMapping("/{machineTypeId}")
    public Result<String> deleteMachineType(
            @Parameter(description = "机器类型ID", required = true) @PathVariable String machineTypeId) {
        try {
            boolean success = machineTypeService.deleteMachineType(machineTypeId);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            log.error("删除机器类型失败：{}", e.getMessage(), e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    @Operation(summary = "批量删除机器类型", description = "根据ID列表批量删除机器类型")
    @DeleteMapping("/batch")
    public Result<String> deleteMachineTypes(@RequestBody List<String> machineTypeIds) {
        try {
            boolean success = machineTypeService.deleteMachineTypes(machineTypeIds);
            if (success) {
                return Result.success("批量删除成功");
            } else {
                return Result.error("批量删除失败");
            }
        } catch (Exception e) {
            log.error("批量删除机器类型失败：{}", e.getMessage(), e);
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }

    @Operation(summary = "获取所有机器类型列表", description = "获取所有机器类型的简要信息，用于下拉选择")
    @GetMapping("/all")
    public Result<List<MachineType>> getAllMachineTypes() {
        try {
            List<MachineType> machineTypes = machineTypeService.getAllMachineTypes();
            return Result.success(machineTypes);
        } catch (Exception e) {
            log.error("获取所有机器类型列表失败：{}", e.getMessage(), e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    @Operation(summary = "检查机器类型名称是否存在", description = "检查机器类型名称是否已被使用")
    @GetMapping("/check-name")
    public Result<Boolean> checkTypeNameExists(
            @Parameter(description = "机器类型名称", required = true) @RequestParam String typeName,
            @Parameter(description = "排除的机器类型ID（更新时使用）") @RequestParam(required = false) String excludeId) {
        try {
            boolean exists = machineTypeService.checkTypeNameExists(typeName, excludeId);
            return Result.success(exists);
        } catch (Exception e) {
            log.error("检查机器类型名称失败：{}", e.getMessage(), e);
            return Result.error("检查失败：" + e.getMessage());
        }
    }
} 